var qyfr = {
    setBtnStatus:function () {
        var row = $('#grid_qyfr').datagrid('getSelected');
        if (row && row.id) {
            $("#btn_qyfr_mod").show();
            $("#btn_qyfr_del").show();
        } else {
            $("#btn_qyfr_mod").hide();
            $("#btn_qyfr_del").hide();
        }
    },
    alwayShowDetail:function (rowData) {
        $('#layout_qyfr').layout('remove', 'center');
        $('#layout_qyfr').layout('add', {
            region:'center',
            split:true,
            href:$.webPath() + '/organ/qyfr/qyfr_detail?qyfrId=' + rowData.id
        });
    },
    showDetail:function (rowData) {
        if ($("#qyfr_detail_id").val() != rowData.id) {
            qyfr.alwayShowDetail(rowData);
        }
    }
}

$(function () {
    //企业法人列表展示
    $("#grid_qyfr").datagrid({
        url:$.webPath() + "/organ/qyfr/qyfrList",
        toolbar:"#toolbar_qyfr",
        fitColumns:true,
        singleSelect:true,
        pageNumber: 1,
        pagination:true,
        fit:true,
        rownumbers:true,
        striped:true,
        columns:[[
            {field:'qyqc', title:'企业全称', width:240},
            {field:'qyjc', title:'简称', width:110},
            {field:'khh', title:'开户行', width:180},
            {field:'fddlr', title:'法定代理人', width:100},
            {field:'nsh', title:'纳税号', width:240},
            {field:'nskhh', title:'纳税开户行', width:120},
            {field:'dh', title:'电话', width:120},
            {field:'bgdz', title:'办公地址', width:240},
            {field:'lx', title:'类型', width:150,
                formatter: function (value) {
                    if (value == '0')
                        return '外部';
                    if (value == '1')
                        return '内部';
                }
            },
            {field:'bz', title:'备注', width:150}
        ]],
        onLoadSuccess:function(){
            $(this).datagrid('selectRow', 0);
            qyfr.setBtnStatus();
        },
        onClickRow:function(){
            qyfr.setBtnStatus();
        },
        onDblClickCell:function (index, field) {
            if (field == "qyqc") {
                qyfr_modify('修改企业法人', 'update_qyfr');
            }
        },
        onSelect:function (rowIndex, rowData) {
            qyfr.showDetail(rowData);
        }
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_qyfr_add").click(function () {
        qyfr_add('新增企业法人', 'add_qyfr');
    });
    //修改按钮
    $("#btn_qyfr_mod").click(function () {
        qyfr_modify('修改企业法人', 'update_qyfr');
    });
    //删除按钮
    $("#btn_qyfr_del").click(function () {
        qyfr_delete();
    });
    //查询按钮
    $("#btn_qyfr_search").click(function () {
        qyfr_search();
    });
});

//新增企业法人
function qyfr_add(title, action) {
    var href = $.webPath() + "/organ/qyfr/qyfr_content";
    var actionUrl = $.webPath() + "/organ/qyfr/" + action;
    qyfr_showDialog(title, href, actionUrl);
}

//修改企业法人
function qyfr_modify(title, action) {
    var record = $("#grid_qyfr").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/organ/qyfr/qyfr_content?qyfrId=" + record.id;
    var actionUrl = $.webPath() + "/organ/qyfr/" + action;
    qyfr_showDialog(title, href, actionUrl);
}

//删除企业法人
function qyfr_delete() {
    var record = $('#grid_qyfr').datagrid('getSelected');
    if (record) {
        $.messager.confirm('提示', '是否删除该企业法人？', function (r) {
            if (r) {
                var index = $('#grid_qyfr').datagrid('getRowIndex', record);
                $.ajax({
                    url: $.webPath() + "/organ/qyfr/delete_qyfr",
                    dataType: 'json',
                    type: 'POST',
                    data: {qyfrId:record.id},
                    beforeSend: function () {
                        $("#layout_qyfr").mask();
                    },
                    success:function(data){
                        $("#layout_qyfr").mask("hide");
                        if(data.statusCode == 200){
                            $('#grid_qyfr').datagrid('deleteRow', index);
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        }else{
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的企业法人！", "warning");
    }
}

//查询企业法人
function qyfr_search() {
    $('#grid_qyfr').datagrid('load',
        {
            qyqc:$('#qyfr_qyqc').val(),
            fddlr:$('#qyfr_fddlr').val(),
            nsh:$('#qyfr_nsh').val()
        }
    );
}

//新增或修改企业法人弹出窗口
function qyfr_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:650,
        height:450,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_qyfr').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            } else {
                                var fj = $("#qyfr-fileQueue input[name='qyfr.fjList.id']").map(function () {
                                    return $(this).val();
                                });
                                for (var i = 0; i < fj.length; i++) {
                                    param['qyfr.fjList[' + i + '].id'] = fj[i];
                                }
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_qyfr').datagrid('reload');
                                $('#layout_qyfr').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ] ,
        onBeforeClose: function () {
            window.parent.destroy('qyfr-uploadify');
        }
    });
}
